#!/usr/bin/env python
#_*_coding:utf-8 _*_
import paramiko,sys,os
import time

def CountClinet(ip,username,passwd,localdir,rotomedir,filename):
	try:
		client = paramiko.SSHClient(0)
		client.set_missing_host_key_policy(paramiko.AutoAddPolicy(0))
		client.set_log_channel(sshlogin.log)
		client.connect(hostname=ip,port=22,username=username,password=passwd)
	except  Exception:
		print u'用户名或密码错误!'
		sys.exit()
	else:
		sshsend = client.invoke_shell()
	try:

		sshsend.send('cd %s','tar zcvf %s %s_%d.tar.gz','find -mtime +1') % (rotomedir,filename,filename,time)
	except Exception:
		print str(ip) + '打包失败！'
		time.sleep()
	try:
		sshsend.send('scp -r *.gz %s@%s:%s') % (username,ip,localdir)
	except Exception:
		print str(ip) + '拷贝失败！'
		time.sleep()
	try:
		sshsend.send('cd %s','rm -rf *.gz') % (rotomedir)
	except Exception:
		print str(ip) + '删除失败!'
	else:
		print str(ip) + '操作成功!'


if __name__ == '__main__':
	f = open('ip.txt','r')
	for f_list in f.readlines():
		ip = f_list.split(':')[0]
		username = f_list.split(':')[1]
		passwd = f_list.split(':')[2]
		localdir = f_list.split(':')[3]
		rotomedir = f_list.split(':')[4]
		filename = f_list.split(':')[5]
		print ip,username,passwd,localdir,rotomedir,filename
		CountClinet(ip,username,passwd,localdir,rotomedir,filename)
	f.close()


